package com.yb.guigu.pick.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yb.guigu.pick.auth.entity.SysUser;
import com.yb.guigu.pick.core.constant.LoginTypeEnum;
import com.yb.guigu.pick.security.bean.GuiguPickUser;
import com.yb.guigu.pick.security.service.IGuiguPickUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Objects;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/6/13 16:56
 * FileName: GuiguPickUserDetailService
 * Description:
 */
public abstract  class AbstractGuiguPickUserDetailsService implements IGuiguPickUserDetailsService {


    @Override
    public GuiguPickUser loadUser(GuiguPickUser guiguPickUser) {

        SysUser sysUser = loadSysUser(guiguPickUser);

        return loadUser(sysUser);
    }

    protected SysUser loadSysUser(GuiguPickUser guiguPickUser){

        String loginType = guiguPickUser.getLoginType();

        if(loginType.equalsIgnoreCase(LoginTypeEnum.USER_PASS.getLoginType())){
            return this.loadByUsername(guiguPickUser.getUsername());
        }

        if(loginType.equalsIgnoreCase(LoginTypeEnum.EMAIL_CODE.getLoginType())){
            return this.loadByEmail(guiguPickUser.getEmail());
        }

        if(loginType.equalsIgnoreCase(LoginTypeEnum.MOBILE_CODE.getLoginType())){
            return this.loadByPhone(guiguPickUser.getPhone());
        }

        return null;
    }


    protected abstract SysUser  loadByUsername(String username);


    protected abstract SysUser loadByEmail(String email);

    protected abstract SysUser loadByPhone(String phone);


    protected abstract GuiguPickUser loadUser(SysUser sysUser);


    @Override
    public final GuiguPickUser loadUserByUsername(String username) throws UsernameNotFoundException {
        throw new UsernameNotFoundException("不支持操作");
    }
}
